﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsCatchCache
    {
        public int Add(KeRui.Model.CmsCatchCache model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@Url", SqlDbType.VarChar, 0xff), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@CatchID", SqlDbType.Int, 4), new SqlParameter("@CatchName", SqlDbType.NVarChar, 50), new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@NodeName", SqlDbType.NVarChar, 50), new SqlParameter("@ContentID", SqlDbType.Int, 4) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Url;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.CatchID;
            parameters[4].Value = model.CatchName;
            parameters[5].Value = model.NodeID;
            parameters[6].Value = model.NodeName;
            parameters[7].Value = model.ContentID;
            DbHelperSQL.RunProcedure("CmsCatchCache_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public bool Exists(string Title, int NodeID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@NodeID", SqlDbType.Int, 4) };
            parameters[0].Value = Title;
            parameters[1].Value = NodeID;
            return (DbHelperSQL.RunProcedure("CmsCatchCache_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.CmsCatchCache GetModel(int NodeID, string Url)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@Url", SqlDbType.VarChar, 0xff) };
            parameters[0].Value = NodeID;
            parameters[1].Value = Url;
            KeRui.Model.CmsCatchCache cache = new KeRui.Model.CmsCatchCache();
            DataSet set = DbHelperSQL.RunProcedure("CmsCatchCache_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    cache.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                cache.Url = set.Tables[0].Rows[0]["Url"].ToString();
                cache.Title = set.Tables[0].Rows[0]["Title"].ToString();
                if (set.Tables[0].Rows[0]["CatchID"].ToString() != "")
                {
                    cache.CatchID = int.Parse(set.Tables[0].Rows[0]["CatchID"].ToString());
                }
                cache.CatchName = set.Tables[0].Rows[0]["CatchName"].ToString();
                if (set.Tables[0].Rows[0]["NodeID"].ToString() != "")
                {
                    cache.NodeID = int.Parse(set.Tables[0].Rows[0]["NodeID"].ToString());
                }
                cache.NodeName = set.Tables[0].Rows[0]["NodeName"].ToString();
                if (set.Tables[0].Rows[0]["ContentID"].ToString() != "")
                {
                    cache.ContentID = int.Parse(set.Tables[0].Rows[0]["ContentID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    cache.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return cache;
            }
            return null;
        }
    }
}

